package com.javasm.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @version 0.1
 * @className: MyBatisHeapler
 * @description:
 * @author: gfs
 * @date: 2025/6/12 10:22
 * @since: jdk17
 */
public class MyBatisHeapler {

    private static SqlSessionFactory sqlSessionFactory;

    static{
        String resource = "mybatis-config.xml";
        try{
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        } catch (IOException e) {
           e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
        //自动提交不开
        //方便多sql操作时 统一控制事务
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }

    public static void backSqlSession(SqlSession sqlSession){
        sqlSession.close();
    }
    public static void backAndSaveSqlSession(SqlSession sqlSession){
        sqlSession.commit();
        sqlSession.close();
    }



}
